package com.dd.cloud.user.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.dd.cloud.user.entity.file.AppVersion;
import com.dd.cloud.user.req.file.AppVersionCreateReq;
import com.dd.cloud.user.req.file.AppVersionQueryTermsReq;
import com.dd.cloud.user.req.file.AppVersionUpdateReq;
import com.dd.cloud.user.service.file.IAppVersionService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;

/**
 * @author czg
 * @description app
 * @date 2023/5/9 17:27
 */
@RestController
@RequestMapping("/app")
@Slf4j
public class AppController {
    @Autowired
    private IAppVersionService appVersionService;


    /**
     * 检查app版本
     *
     * @param appId
     * @param version
     * @param type
     * @return
     */
    @GetMapping("/version/check")
    public ResponseEntity<AppVersion> checkAppVersion(@NotBlank(message = "appid不能为空") String appId,
                                                      @NotBlank(message = "app版本不能为空") String version,
                                                      @NotNull(message = "app平台不能为空") Integer type) {
        AppVersion appVersion = appVersionService.checkAppVersion(appId, version, type);
        return ResponseEntity.ok(appVersion);
    }

    /**
     * 分页查询app版本信息
     *
     * @param req
     * @return
     */
    @GetMapping("/version/page")
    public ResponseEntity<IPage<AppVersion>> checkAppVersion(@Valid AppVersionQueryTermsReq req) {
        IPage<AppVersion> appVersionIPage = appVersionService.queryAppVersionPageTerms(req);
        return ResponseEntity.ok(appVersionIPage);
    }

    /**
     * 添加app版本
     *
     * @param req
     * @return
     */
    @PostMapping("/version/add")
    public ResponseEntity<Void> addAppVersion(@Valid @RequestBody AppVersionCreateReq req) {
        appVersionService.addAppVersion(req);
        return ResponseEntity.ok().build();
    }

    /**
     * 修改app版本
     *
     * @param req
     * @return
     */
    @PostMapping("/version/update")
    public ResponseEntity<Void> updateAppVersion(@Valid @RequestBody AppVersionUpdateReq req) {
        appVersionService.updateAppVersion(req);
        return ResponseEntity.ok().build();
    }

    /**
     * 修改app版本状态
     *
     * @param id
     * @return
     */
    @PostMapping("/version/update/status")
    public ResponseEntity<Void> updateAppVersionStatus(@NotNull(message = "版本id不能为空") Integer id,
                                                       @NotNull(message = "状态不能为空") Integer status) {
        appVersionService.updateAppVersionStatus(id, status);
        return ResponseEntity.ok().build();
    }

    /**
     * app下载次数
     *
     * @param version app版本
     * @param type app下载渠道 1链接下载 2自动更新
     * @return
     */
    @PostMapping("/download/num/add")
    public ResponseEntity<Void> downloadNumAdd(@NotBlank(message = "版本不能为空") String version,
                                                       @NotNull(message = "下载类型不能为空") Integer type) {
        appVersionService.updateAppDownloadNum(version, type);
        return ResponseEntity.ok().build();
    }
}
